﻿using Core;
using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string GetListUser(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách người dùng" });
            try
            {
                tbUsers_Proxy pr = new tbUsers_Proxy();
                List<DataAccess.tbUsers> list = new List<DataAccess.tbUsers>();
                int totalitem = 0;
              
                if (keyword == "null" || keyword == "")
                {
                    list = pr.SelectAlltbUsers_Proxy();
                    totalitem = list.Count;
                }
                else
                {
                    list = (pr.SelectNo_tbUsers_Proxy(keyword)).ToList<DataAccess.tbUsers>();
                }

                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage).Select(x => new
                {
                    x.UserID,
                    x.UserName,
                    x.Password,
                    x.FullName,
                    x.Birthday,
                    x.Address,
                    x.Phone,
                    x.Mobile,
                    x.Email,
                    x.CreatedDate,
                    x.LastLogin,
                    x.IsAdmin,
                    x.Active,
                    x.Note,
                    x.ResetCode,
                    x.DeadlineReset
                    //= x.CreateDate.ToString("U"),
                });
                return new JavaScriptSerializer().Serialize(new { Status = true, Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

        [WebMethod]
        public static string DeleteUser(int Id)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa người dùng" });
            try
            {
                tbUsers_Proxy pr = new tbUsers_Proxy();
                if (pr.Delete_tbUsers_Proxy(Id))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string InsertUser(string Password, string FullName, string Email, string Address, string Phone, string Mobile, string Note, string ResetCode, string UserName)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm người dùng" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
                tbUsers_Proxy pr = new tbUsers_Proxy();
                tbUsers user = new tbUsers();
                user.UserID = 0;
                user.UserName = ConvertType.ToString(UserName);
                user.Password = ConvertType.ToString(Password);
                user.FullName = ConvertType.ToString(FullName);
                user.Birthday = DateTime.Now;
                user.Address = ConvertType.ToString(Address);
                user.Phone = ConvertType.ToString(Phone);
                user.Mobile = ConvertType.ToString(Mobile);
                user.Email = ConvertType.ToString(Email);
                user.IsAdmin = false;
                user.Active = true;
                user.Note = Note;
                user.ResetCode = ConvertType.ToString(ResetCode);
                user.CreatedDate = DateTime.Now;
                user.LastLogin = DateTime.Now;
                user.DeadlineReset = DateTime.Now;

                if (pr.Insert_tbUsers_Proxy(user))
                    log.Info(UserLogin.GetUserName + "Thêm mới user  thành công");
                return new JavaScriptSerializer().Serialize(new { Status = true });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string UpdateUser(int UserID, string FullName, string Email, string Address, string Phone, string Mobile, string Pass)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa người dùng" });
            ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
            try
            {
                tbUsers_Proxy pr = new tbUsers_Proxy();
                tbUsers user = pr.GetById_tbUsers_Proxy(UserID).ToObject<tbUsers>();
                if (user != null && user.Password.Equals(EncryptData.Encryption(Pass)))
                {
                    user.FullName = ConvertType.ToString(FullName);
                    user.Address = ConvertType.ToString(Address);
                    user.Phone = ConvertType.ToString(Phone);
                    user.Mobile = ConvertType.ToString(Mobile);
                    user.Email = ConvertType.ToString(Email);
                }
                else
                {
                    return new JavaScriptSerializer().Serialize(new { Status = false });
                }
                tbUsers_Proxy proxy = new tbUsers_Proxy();
                if (proxy.Update_tbUsers_Proxy(user))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

       
        [WebMethod]
        public static string UpdateUser(int UserID, string UserName, string FullName, string Email, bool Active)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa người dùng" });
            ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
            try
            {
                tbUsers_Proxy pr = new tbUsers_Proxy();
                tbUsers user = pr.GetById_tbUsers_Proxy(UserID).ToObject<tbUsers>();
                user.Active = Active;
                user.Email = Email;
                user.FullName = FullName;
                user.UserName = UserName;
                tbUsers_Proxy proxy = new tbUsers_Proxy();
                if (proxy.Update_tbUsers_Proxy(user))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

        [WebMethod]
        public static string AddNewUser(string UserName, string FullName, string Email, string Password, bool Active)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền gthêm người dùng" });
            ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
            try
            {
                tbUsers_Proxy pr = new tbUsers_Proxy();
                tbUsers user = new tbUsers()
                {
                    Active = Active,
                    Email = Email,
                    FullName = FullName,
                    Password = DataAccess.EncryptData.Encryption(Password),
                    UserName = UserName
                };
                user.IsAdmin = false;
                tbUsers_Proxy proxy = new tbUsers_Proxy();
                if (proxy.Insert_tbUsers_Proxy(user))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

    }
}